package com.genesis.web.controller.omsorder;


import com.genesis.mall.common.Result;
import com.genesis.mall.dto.OmsOrderDto;
import com.genesis.mall.dto.OmsOrderVO;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.OmsOrder;
import com.genesis.mall.service.IOmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
//@RestController
//@RequestMapping("/order")
public class OmsOrderController {

    @Autowired
    private IOmsOrderService omsOrderService;

    /**
     * 查询订单
     *
     * @param orderParam
     * @param pageNum
     * @param pageSize
     * @return    完成
     */
    @GetMapping("/list")
    public Result<PageDto<OmsOrder>> list(OmsOrderVO orderParam, Integer pageNum, Integer pageSize) {

        return Result.success(omsOrderService.findList(orderParam, pageNum, pageSize));

    }

    /**
     * 批量删除订单
     * @param ids
     * @return   完成
     */
    @PostMapping("/delete")
    @Transactional
    public Result  delete(@RequestParam List<Long> ids){
        omsOrderService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 批量关闭订单
     * @param ids
     * @param note
     * @return
     */
    @PostMapping("/update/close")
    @Transactional
    public Result closeOrders(@RequestParam List<Long> ids ,
                              @RequestParam String note,
                              HttpServletRequest request){

        omsOrderService.closeOrders(ids,note,request);
        return Result.success();
    }

    /**
     * 批量发货
     * @param deliveryParamList
     * @return  完成
     */
    @PostMapping("/update/delivery")
    @Transactional
    public Result delivery(@RequestBody List<OmsOrder> deliveryParamList){
        omsOrderService.delivery(deliveryParamList);
        return Result.success();
    }


    /**
     * 修改订单费用信息
     * @param discountAmount
     * @param freightAmount
     * @param orderId
     * @param status
     * @return
     */

    @PostMapping("/update/moneyInfo")
    public Result moneyInfo(@RequestParam BigDecimal discountAmount,
                            @RequestParam BigDecimal freightAmount,
                            @RequestParam Long orderId,
                            @RequestParam Integer status){
        boolean flag = omsOrderService.moneyInfo(discountAmount, freightAmount, orderId, status);
        if (flag){
            return Result.success("修改成功");
        }
        return Result.error("非法操作");
    }

    /**
     * 备注订单
     * @param id
     * @param note
     * @param status
     * @return   完成
     */
    @PostMapping("/update/note")
    public Result note(@RequestParam Long id,
                       @RequestParam String note,
                       @RequestParam Integer status){

        omsOrderService.note(id,note,status);
        return Result.success();
    }

    /**
     * 修改收货人信息
     * @param omsOrder
     * @return   完成
     */
    @PostMapping("/update/receiverInfo")
    public Result receiverInfo(@RequestBody OmsOrder omsOrder){

        omsOrderService.receiverInfo(omsOrder);
        return Result.success();
    }

    /**
     * 获取订单详情
     * @param id
     * @return   完成
     */
    @GetMapping("/{id}")
    public Result<OmsOrderDto> getAllInfoById(@PathVariable Long id){
           OmsOrderDto omsOrderDto = omsOrderService.getAllInfoById(id);
           return Result.success(omsOrderDto);
    }


}
